package com.partners.controller.Page;

import com.alibaba.fastjson.JSONObject;
import com.partners.annotation.Auths;
import com.partners.configure.wechatmp.WxMpConfiguration;
import com.partners.configure.wechatmp.WxMpProperties;
import com.partners.constant.PermissionEnum;
import com.partners.service.UserService;
import com.partners.utils.CodeUtils;
import com.partners.utils.SessionManager;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author liangyongle
 * @date 2018-12-21
 */


@Controller
@RequestMapping("orderPage")
@Slf4j
public class OrderPageController {
    @Resource
    private SessionManager sessionManager;

    @Resource
    private UserService userService;

    @Resource
    private WxMpProperties wxMpProperties;


    /**
     * 优惠开单
     * @param session HttpSession
     * */
    @GetMapping("create_order")
    @Auths
    public ModelAndView createOrder(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        Integer code = PermissionEnum.CREATE_ORDER.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/order_add";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        mav.addObject("uid",uid);
        return mav;

    }

    /**
     * 成交记录
     * @param session HttpSession
     * */
    @GetMapping("order_list")
    @Auths
    public ModelAndView getOrderList(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        ModelAndView mav =new ModelAndView("show/order_list");
        mav.addObject("uid",uid);
        return mav;
    }

   /**
   * 获取订单详情
   * */
    @GetMapping("order/{id}")
    public ModelAndView getOrderDetail(@PathVariable("id") String id){
        ModelAndView mav =new ModelAndView("show/order_detail");
        mav.addObject("id",id);
        return mav;
    }

    /**
     * 测试下订单
     * */
    @GetMapping("payOrder")
    public ModelAndView payOrder(){
        ModelAndView mav =new ModelAndView("show/pay");
        mav.addObject("appId",wxMpProperties.getBaseAppId());
        return mav;
        //WxJsapiSignature jsapiSignature;
//        final WxMpService wxMpService = WxMpConfiguration.getMpServices().get(appId);
//        try {
//            jsapiSignature = wxMpService.createJsapiSignature("http://lyl.natapp1.cc/taskPage/taskinfo?id=5");
//            ModelAndView mav =new ModelAndView("show/pay");
//            mav.addObject("appId",jsapiSignature.getAppId());
//            mav.addObject("nonceStr",jsapiSignature.getNonceStr());
//            mav.addObject("timestamp",String.valueOf(jsapiSignature.getTimestamp()));
//            mav.addObject("signature",jsapiSignature.getSignature());
//            mav.addObject("url",jsapiSignature.getUrl());
//            return mav;
//        } catch (WxErrorException e) {
//            e.printStackTrace();
//            return null;
//        }
    }




}
